﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace LCL.Tools.WinFrm
{
    public partial class StringTools : BaseFrm
    {
        public StringTools()
        {
            InitializeComponent();
        }
        //拼接字符串
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                #region 拼接字符串
                if (txtContent.Text.Trim() != "")
                {
                    StringBuilder l = new StringBuilder();
                    l.AppendLine("StringBuilder builder = new StringBuilder();");
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                    string filename = System.Windows.Forms.Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);
                    while (true)
                    {
                        string currentLine = read.ReadLine();
                        if (currentLine == null)
                        {
                            break;
                        }
                        l.AppendLine("builder.AppendLine(\"" + currentLine.Replace("\"", "\\\"") + " \");");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        int i = 0;
        // 大小写转换
        private void button2_Click(object sender, EventArgs e)
        {
            string temp = txtContent.Text;
            if (i == 0)
            {
                txtContent.Text = temp.ToUpper();
                i = 1;
            }
            else
            {
                txtContent.Text = temp.ToLower();
                i = 0;
            }
        }

        private void txtContent_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                //复制：
                if (txtContent.SelectedText != "")
                    Clipboard.SetDataObject(txtContent.SelectedText);
            }
            else if (e.KeyCode == Keys.F12)
            {

            }
            else if ((!e.Alt && e.Control) && (e.KeyCode == Keys.C))
            {

            }
        }
        //生成属性
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                #region 拼接字符串
                if (txtContent.Text.Trim() != "")
                {
                    StringBuilder l = new StringBuilder();
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

                    string filename = System.Windows.Forms.Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);

                    while (true)
                    {
                        string currentLine = read.ReadLine();
                        if (currentLine == null)
                        {
                            break;
                        }
                        l.AppendLine(" public string " + currentLine.Replace("\"", "\\\"") + " ;");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //javascript
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                #region 拼接字符串
                if (txtContent.Text.Trim() != "")
                {
                    StringBuilder l = new StringBuilder();
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

                    string filename = System.Windows.Forms.Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);

                    while (true)
                    {
                        string currentLine = read.ReadLine();
                        if (currentLine == null)
                        {
                            break;
                        }
                        l.AppendLine("document.write(\"" + currentLine.Replace("\"", "'") + "\");");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            }
            catch
            {
                #region 拼接字符串
                if (txtContent.Text.Trim() != "")
                {
                    string temp = txtContent.Text
                        .Replace("\"", "'")
                        .Replace(" ", "")
                        .Replace("\n", "")
                        .Replace("\r", "")
                        .Replace("\r\n", "")
                        .Trim();
                    txtContent.Text = temp;
                }
                #endregion
            }
        }
        //Switch Case转换
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                #region 拼接字符串
                if (txtContent.Text.Trim() != "")
                {
                    StringBuilder l = new StringBuilder();
                    l.AppendLine("StringBuilder builder = new StringBuilder();");
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

                    string filename = System.Windows.Forms.Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);

                    while (true)
                    {
                        string currentLine = read.ReadLine();
                        if (currentLine == null)
                        {
                            break;
                        }
                        l.AppendLine("case \"" + currentLine.Replace("\"", "\\\"") + " \":");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //反拼接字符串
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                #region 读取StringBuilder里的数据
                if (txtContent.Text.Trim() != "")
                {
                    StringBuilder lismsg = new StringBuilder();
                    string strContent = txtContent.Text;
                    string pattern = @"\(""(?<dd>.*?)""\)";
                    var reg = new Regex(pattern);
                    MatchCollection mc = reg.Matches(strContent);
                    foreach (Match m in mc)
                    {
                        lismsg.AppendLine(m.Groups[1].Value);
                    }
                    txtContent.Text = lismsg.ToString();
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
